<?php
/**
 *
 * Upload.php
 *
 * 上传组件
 *
 * @author     limin<lmin@vip.deyi.com>
 * @since      2015/7/15 14:20
 * @copyright  deyi.com
 *　
 */

namespace Deyi\Components;


use Apps\Common\Constant\ExceptionConstant;

class Upload
{
    protected $_file;

    protected $_fileName;
    protected $_fileExt = null;
    protected $_fileMime = null;
    protected $_fileSuffix;

    protected $_savePath;
    protected $_saveRootPath;
    protected $_saveRelativePath;

    protected $_saveAbsPath = null;
    protected $_saveAbsFilename = null;
    protected $_saveRelativeFilename = null;

    protected $_result;

    public function __construct()
    {

    }

    public function save()
    {
        $savePath = $this->getSaveAbsPath();
        $this->checkDirExsits($savePath);

        $destFile = $savePath. $this->getFileName();
        $result = move_uploaded_file($this->_file['tmp_name'], $destFile);

        if ($result) {
            $this->_saveAbsFilename = $destFile;
            $this->_saveRelativeFilename = $this->getSaveRelativePath(). $this->getFileName();
        }

        return $result;
    }

    public function setFile($file)
    {
        $this->_file = $file;
        return $this;
    }

    public function getType()
    {
        $file = is_array($this->_file) && array_key_exists('tmp_name', $this->_file) ? $this->_file['tmp_name'] : null;
        return $this->getFileMime($file);
    }

    protected function getFileMime($file)
    {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        return finfo_file($finfo, $file);
    }

    protected function setFileMime($mime)
    {
        $this->_fileMime = $mime;
        return $this;
    }

    /**
     * 获取上传文件扩展名
     * @return string
     */
    public function getFileExt()
    {
        if (!$this->_fileExt) {
            $fileExt = '';
            switch ($this->getType()) {
                case 'image/jpg':
                case 'image/jpeg':
                case 'image/pjpeg':
                    $fileExt = 'jpg';
                    break;
                case 'image/png':
                case 'image/x-png':
                    $fileExt = 'png';
                    break;
                case 'image/gif':
                    $fileExt = 'gif';
                    break;
            }
            $this->_fileExt = $fileExt;
        }

        return $this->_fileExt;
    }

    public function setFileExt($fileExt)
    {
        $this->_fileExt = $fileExt;
        return $this;
    }

    public function getSize()
    {
        return static::getFileMime($this->_file);
    }

    protected function getTmpFile()
    {
        return $this->_file['tmp_name'];
    }

    public function setSaveRootPath($rootPath)
    {
        $this->_saveRootPath = $rootPath;
        return $this;
    }

    public function getSaveRootPath()
    {
        return $this->_saveRootPath;
    }

    public function setSaveRelativePath($path)
    {
        $this->_saveRelativePath = $path;
        return $this;
    }

    public function getSaveRelativePath()
    {
        return $this->_saveRelativePath;
    }

    public function setSaveAbsPath($absPath)
    {
        $this->_saveAbsPath = $absPath;
        return $this;
    }

    public function getSaveAbsPath()
    {
        if ($this->_saveAbsPath === null) {
            $this->_saveAbsPath = $this->_saveRootPath. $this->_saveRelativePath;
        }
        return $this->_saveAbsPath;
    }

    public function setFileName($fileName)
    {
        $this->_fileName = $fileName;
        return $this;
    }

    public function getFileName()
    {
        return $this->_fileName . $this->_fileSuffix . '.' . $this->getFileExt();
    }

    public function getSavedAbsFilename()
    {
        return $this->_saveAbsFilename;
    }

    public function getSavedRelativeFilename()
    {
        return $this->_saveRelativeFilename;
    }

    public function getSavedFileInfo()
    {
        $info['name'] = $this->_file['name'];
        $info['type'] = $this->_file['type'];
        $info['size'] = $this->_file['size'];
        $info['relative_filename'] = $this->_saveRelativeFilename;
        $info['absolute_filename'] = $this->_saveAbsFilename;
        return $info;
    }

    /**
     * 检测是否为图片类型
     * @param null $typeList　参数为数组，['jpg','png']
     * @return bool
     */
    public function isImage($typeList = null)
    {
        $typeList = is_array($typeList) ? $typeList : ['jpg', 'png', 'gif'];
        return in_array($this->getFileExt(), $typeList) ? true : false;
    }


    protected function checkDirExsits($path)
    {
        if (!is_dir($path) && !mkdir($path, 0777, true)) {
            throw new \Exception('创建目录失败', ExceptionConstant::INTERNAL_ERROR);
        }
    }
}
